package com.atguigu.gmall.cdc.utils;

import com.atguigu.gmall.cdc.vo.StudentVO;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: liminghui
 * @date: 2021/10/30 0:00
 * @version: 1.0
 * @description:
 */
public class CommonUtil {

    /**
     * id=7,name=zhshan,age=28 ==>studentvo
     *
     * @param before
     * @return
     */
    public static StudentVO parseStrToStuVO(String before) {
        String[] split = before.split(",");
        int id = Integer.parseInt(split[0].split("=")[1]);
        String name = split[1].split("=")[1];
        int age = Integer.parseInt(split[2].split("=")[1]);
        StudentVO studentVO = new StudentVO(id, name, age);
        return studentVO;
    }

    // 参考 正则数据 https://www.cnblogs.com/zaixiachengxuyuan/p/14465492.html
    public static String parseMysqlCDCData(String input, String regex) {
        Pattern p1 = Pattern.compile(regex);
        Matcher matcher = p1.matcher(input);

        while (matcher.find()) {
            String result = matcher.group(1); // before=Struct{id=7,name=zhshan,age=28}
            return result;
        }
        return null;
    }
}
